﻿namespace PowerSong.SongDatabase
    {
    using System.Collections.Generic;
    using System.IO;

    internal class Configuration : BaseEntity
        {

        internal Dictionary<string, string> Values { get; private set; }

        public Configuration()
            : base(FileEntryType.Configuration, 1)
            {
            this.Values = new Dictionary<string, string>();
            }

        internal override bool Deserialise(Database database, BinaryReader reader, int version)
            {

            // Ensure the version is correct
            if (version != 1) return false;

            // Get the set of settings
            this.Values.Clear();
            var settingCount = reader.ReadInt32();
            for (var i = 0; i < settingCount; i++)
                {
                var name = reader.ReadString();
                var value = reader.ReadString();
                Values[name] = value;
                }

            // Details read in successfully
            return true;

            }

        internal override void Serialise(BinaryWriter writer)
            {

            // Write the set of settings
            writer.Write(Values.Count);
            foreach (var entry in Values)
                {
                writer.Write(entry.Key);
                writer.Write(entry.Value);
                }

            }

        public override string ToString()
            {
            return Values.Count + " Settings";
            }

        }

    }
